﻿using Models.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Web.Common;
using Web.Filters;

namespace Web.Controllers.API
{
    [ApiAuthentication]
    public class DefaultController : ApiController
    {
        private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        [AllowAnonymous,HttpPost]
        public IHttpActionResult Login([FromBody]Models.API.Param.LoginModel entity)
        {
            Result<Models.API.Result.LoginModel> result = new Result<Models.API.Result.LoginModel>() { Type = AjaxResultType.Error, Content = "", TotalRows = 0, Data = new Models.API.Result.LoginModel() };
            
            if (entity == null)
            {
                result.Content = "传输参数无效";
                return Json(result);
            }
            else if (String.IsNullOrWhiteSpace(entity.LoginName) || String.IsNullOrWhiteSpace(entity.LoginPass))
            {
                result.Content = "传输参数无效";
                return Json(result);
            }
            try
            {
                DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("select Logins.ID,Users.Token,Logins.LoginPass,Logins.UserID,Users.Name UserName from Logins inner join Users on Logins.UserID=Users.ID where Logins.LoginType=1 and Logins.LoginName='" + entity.LoginName + "' and Logins.IsDeleted=0 and Users.IsDeleted=0").Tables[0];
                if (dt.Rows.Count != 1)
                {
                    result.Content = "登录名错误";
                }
                else if (dt.Rows[0]["LoginPass"].ToString() != entity.LoginPass)
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = "密码错误";
                }
                else
                {
                    result.Data.LoginID = Convert.ToInt32(dt.Rows[0]["ID"]);
                    result.Data.UserID = Convert.ToInt32(dt.Rows[0]["UserID"]);
                    result.Data.UserName = Convert.ToString(dt.Rows[0]["UserName"]);
                    result.Data.LoginName = entity.LoginName;
                    result.Data.Token = Convert.ToString(dt.Rows[0]["Token"]);
                    if (result.Data.Token == "" || FreeADO.DBUtility.DbHelperSQL.GetSingle("select OptionValue from Options where OptionKey='ApiLoginOnly'").ToString() == "1")
                    {
                        result.Data.Token = BaseWebApiExt.GetNewToken(result.Data.UserID);
                    }
                    result.Data.Token = BaseWebApiExt.ToCiphertextToken(result.Data.Token);
                    result.Content = "登录成功";
                    result.Type=AjaxResultType.Success;
                }
                return Json(result);

            }
            catch (Exception e)
            {
                result.Content = e.Message;
                return Json(result);
            }
        }
        [HttpPost]
        public IHttpActionResult test()
        {
            var UserID = BaseWebApiExt.GetUserIDByToken();
            Result<string> result = new Result<string>() { Type = AjaxResultType.Success, Content = "成功", TotalRows = 0, Data = UserID.ToString() };
            return Json(result);
        }
        [HttpPost, AllowAnonymous,HttpGet]
        public string aaaaa()
        {
            return "aaaa";
        }

    }
}
